package testGUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenuBar;
import javax.swing.JSplitPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import net.miginfocom.swing.MigLayout;

import javax.swing.BoxLayout;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;

import javax.swing.JInternalFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;

import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.Normalizer.Form;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.swing.JTextPane;
import javax.swing.JTextArea;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.TextField;
import java.awt.Color;

import javax.swing.ImageIcon;

import java.awt.Canvas;
import java.awt.Toolkit;

import javax.swing.JList;

import java.awt.TextArea;

import javax.swing.JProgressBar;
import javax.swing.JRadioButton;

import java.awt.Component;

import javax.swing.Box;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

public class mainForm extends JFrame {

	private JPanel contentPane;
	private JButton btnRefresh;
	private static Jedis jedis;
	private JPanel panel;
	private JTextField textField_1;
	private JLabel lblServerConnectedTo;
	private static String redisHost = "";
	private JMenuBar menuBar;
	private JMenu mnRefresh;
	private JMenuItem mntmRefresh;
	private static JTextField groupNotice;
	private JLabel lblNewLabel;
	private static JTextField serverGood;
	private JLabel lblNewLabel_1;
	private static JTextField groupBad;
	private static JTextField serverBad;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private static JTextArea txtGoodServers;

	static String keyServerGood = "prod.nw.stats.server.good";
	static String keyGroupGood = "prod.nw.stats.group.good";
	static String keyServerBad = "prod.nw.stats.server.failure";
	static String keyGroupBad = "prod.nw.group.failures";

	static String keyGroupAlert = "prod.nw.stats.group.alert";
	static String keyGroupNotice = "prod.nw.stats.group.notice";
	static String keyServerFailureTimed = "prod:backup-failureTimed:nw:eu:server#";


	private static Timer displayTimer;

	private JLabel lblTopGroups;
	private static JTextArea textTop10ServerBad;
	private static JTextField txtLastRefresh;
	private JLabel lblTopServer;
	private JMenuItem mntmEditAlerts;
	private static JButton buttonError;

	UpdateAlerts frame1;
	private static JLabel lblErrorsInThe;
	private JTextField textRefreshField;
	private static int refreshVal = 10000;
	JScrollPane scrollPane;
	static JTextArea textAreaAlertList;
	private JLabel lblNewLabel_4;
	private static JTextField textFieldBackupCount;
	private static JTextField textFieldTotalCount;
	private static JTextField textFieldFileCount;
	private JLabel lblGServercount;
	private JLabel lblFileCount;
	private static JTextField textFieldFullCount;
	private static JTextField textFieldIncrCount;
	private JLabel lblFullCount;
	private JLabel lblIncrCount;
	Frame3 f2;
	private JButton button;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainForm frame = new mainForm();
					UpdateAlerts frame1 = new UpdateAlerts();

					frame.setVisible(true);



				} catch (Exception e) {
					e.printStackTrace();
				}
			}



		});


		redisHost = "rp-1";
		redisHost = "172.18.38.14";
		jedis = new Jedis(redisHost); 

		ActionListener listener = new ActionListener(){
			public void actionPerformed(ActionEvent event){
				updateFields();
			}
		};
		displayTimer = new Timer(refreshVal, listener);
		displayTimer.start();	
		

	}

	/**
	 * Create the frame.
	 */
	public mainForm() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\personal\\ecplise.workspace\\testGUI\\images\\Backup.jpg"));
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
				updateFields();
				//String temp = Integer.toString(refreshVal);
				textRefreshField.setText(Integer.toString(refreshVal));
			}
			@Override
			public void windowClosing(WindowEvent e) {
				if (f2 !=null) {
					f2.dispose();
				}
			}
		});
		setTitle("Networker Stats");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 682, 640);

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnRefresh = new JMenu("Refresh");
		menuBar.add(mnRefresh);

		mntmRefresh = new JMenuItem("Refresh");
		mntmRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateFields();
			}
		});
		mnRefresh.add(mntmRefresh);

		mntmEditAlerts = new JMenuItem("Edit Alerts");
		mntmEditAlerts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame1.setVisible(true);
			}
		});

		mnRefresh.add(mntmEditAlerts);
		contentPane = new JPanel();

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		btnRefresh = new JButton("Refresh");
		btnRefresh.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				updateFields();
			}
		});





		panel = new JPanel();

		lblServerConnectedTo = new JLabel("Server Connected to");
		panel.add(lblServerConnectedTo);

		textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(10);

		textField_1.setText(redisHost);

		groupNotice = new JTextField();
		groupNotice.setColumns(10);

		lblNewLabel = new JLabel("Group Notice");

		serverGood = new JTextField();
		serverGood.setColumns(10);

		lblNewLabel_1 = new JLabel("Server Good Count");

		groupBad = new JTextField();
		groupBad.setColumns(10);

		serverBad = new JTextField();
		serverBad.setColumns(10);

		lblNewLabel_2 = new JLabel("Group Bad");

		lblNewLabel_3 = new JLabel("Server Bad");

		txtGoodServers = new JTextArea();
		txtGoodServers.setText("\r\n");

		lblTopGroups = new JLabel("Top 10 Server Good");

		textTop10ServerBad = new JTextArea();

		txtLastRefresh = new JTextField();
		txtLastRefresh.setEditable(false);
		txtLastRefresh.setText("Last Refresh = Never");
		txtLastRefresh.setColumns(10);

		lblTopServer = new JLabel("Top 10 Server Bad");

		lblErrorsInThe = new JLabel("Errors in the 24hr hours");
		lblErrorsInThe.setBackground(Color.WHITE);
		
		textRefreshField = new JTextField();
		textRefreshField.setColumns(10);
		
		JButton btnNewButton = new JButton("Reset");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				refreshVal = Integer.parseInt(textRefreshField.getText());
				displayTimer.setInitialDelay(refreshVal);
				displayTimer.restart();
			}
		});
		
		scrollPane = new JScrollPane();
		
		lblNewLabel_4 = new JLabel("Backup Count");
		
		textFieldBackupCount = new JTextField();
		textFieldBackupCount.setColumns(10);
		
		textFieldTotalCount = new JTextField();
		textFieldTotalCount.setColumns(10);
		
		textFieldFileCount = new JTextField();
		textFieldFileCount.setColumns(10);
		
		lblGServercount = new JLabel("G ServerCount");
		
		lblFileCount = new JLabel("File Count");
		
		textFieldFullCount = new JTextField();
		textFieldFullCount.setColumns(10);
		
		textFieldIncrCount = new JTextField();
		textFieldIncrCount.setColumns(10);
		
		lblFullCount = new JLabel("Full Count");
		
		lblIncrCount = new JLabel("Incr Count");
		
		JButton btnNewButton_1 = new JButton("AppWin");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					appWin f2 = new appWin();
					f2.main(null);
					
					
					//f2.setVisible(true);
					//f2.
					
			}
		});
		
		button = new JButton("Nothing Set");

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(10)
							.addComponent(txtLastRefresh, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
							.addGap(90)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(btnRefresh))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblNewLabel_4)
							.addGap(18)
							.addComponent(textFieldBackupCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(221)
							.addComponent(textRefreshField, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnNewButton))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(48)
							.addComponent(lblNewLabel)
							.addGap(58)
							.addComponent(groupNotice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(58)
							.addComponent(lblNewLabel_2)
							.addGap(18)
							.addComponent(groupBad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(48)
							.addComponent(lblNewLabel_1)
							.addGap(28)
							.addComponent(serverGood, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(55)
							.addComponent(lblNewLabel_3)
							.addGap(18)
							.addComponent(serverBad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblTopGroups)
							.addGap(164)
							.addComponent(lblTopServer)
							.addGap(67)
							.addComponent(lblErrorsInThe))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(txtGoodServers, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
							.addGap(34)
							.addComponent(textTop10ServerBad, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
							.addGap(29)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(29, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblGServercount)
						.addComponent(lblFileCount)
						.addComponent(lblFullCount)
						.addComponent(lblIncrCount))
					.addGap(50)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(textFieldFileCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 324, Short.MAX_VALUE)
							.addComponent(btnNewButton_1)
							.addGap(26))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(textFieldTotalCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(439, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldFullCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldIncrCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 326, Short.MAX_VALUE)
							.addComponent(button)
							.addGap(24))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(11)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(txtLastRefresh, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnRefresh))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(6)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(1)
									.addComponent(textRefreshField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addComponent(btnNewButton)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_4)
								.addComponent(textFieldBackupCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(6)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel))
						.addComponent(groupNotice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_2))
						.addComponent(groupBad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_1))
						.addComponent(serverGood, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_3))
						.addComponent(serverBad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(7)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblTopGroups)
						.addComponent(lblTopServer)
						.addComponent(lblErrorsInThe))
					.addGap(11)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(txtGoodServers, GroupLayout.PREFERRED_SIZE, 291, GroupLayout.PREFERRED_SIZE)
						.addComponent(textTop10ServerBad, GroupLayout.PREFERRED_SIZE, 291, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE)
							.addGap(16)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(textFieldTotalCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblGServercount))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(textFieldFileCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblFileCount))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(textFieldFullCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblFullCount))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(textFieldIncrCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblIncrCount)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(31)
							.addComponent(btnNewButton_1)
							.addGap(14)
							.addComponent(button)))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		
		textAreaAlertList = new JTextArea();
		scrollPane.setViewportView(textAreaAlertList);
		contentPane.setLayout(gl_contentPane);
	}


	public static String getfails(String key) {
		return jedis.zcount(key, 0, 10).toString();
	}
	
	public static String getBackupCount(String key) {
		return jedis.get(key);
	}

	public static String getAlerts() {
		Set<String> l = jedis.keys(keyServerFailureTimed + "*");

		String ss;
		ss = "";

		for (String s : l) {

			ss = ss + jedis.get(s) + ":" + jedis.ttl(s) + "\n";
		}

		if (ss == "") {
			ss = "No Alerts";
		}
		return ss;
	}


	private static void updateFields() {
			displayTimer.restart();
		groupNotice.setText(getfails(keyGroupGood));
		serverGood.setText(getfails(keyServerGood));
		groupBad.setText(getfails(keyGroupBad));
		serverBad.setText(getfails(keyServerBad));

		//Set<String> l = jedis.zrevrange(keyServerGood, 0, 9);
		Set<Tuple> l = jedis.zrevrangeWithScores("prod:backup-stats-count:nw:eu#files", 0, 20);
		String ss ="";
		for (Tuple s : l) {
			ss = ss + (int) s.getScore() + ":" + s.getElement() + "\n";
		}
		txtGoodServers.setText(ss);

		l = jedis.zrevrangeWithScores(keyServerBad, 0, 9);
		ss ="";
		for (Tuple s : l) {
			ss = ss + (int) s.getScore() + ":" + s.getElement() + "\n";
		}
		textTop10ServerBad.setText(ss);

		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();

		txtLastRefresh.setText(dateFormat.format(date));

		//AlertList.setText(getAlerts());
		textAreaAlertList.setText(getAlerts());
		
		textFieldBackupCount.setText(getBackupCount("prod:backup-stats-seq:nw:eu"));
		
		textFieldTotalCount.setText(jedis.zcard("prod:backup-stats-count:nw:eu#files").toString());
		
		textFieldFileCount.setText(jedis.get("prod:backup-stats-count:nw:eu:total#files"));
		
		textFieldFullCount.setText(jedis.get("prod:backup-stats-count:nw:eu:total#full"));
		textFieldIncrCount.setText(jedis.get("prod:backup-stats-count:nw:eu:total#incr"));
		
		
		
		//jedis.keys(pattern)
		
		//buttonError.setVisible(false);
		//if ((AlertList.getText()) != null) {
		//	buttonError.setVisible(false);
		//} else 
		//	buttonError.setVisible(true);

	}
}
